﻿package com.tudou.ad.track
{
    import com.tudou.ad.consts.*;
    import com.tudou.ad.interfaces.*;
    import com.tudou.home.model.*;
    import com.tudou.home.track.*;
    import com.tudou.model.*;
    import com.tudou.track.*;

    public class TdAdTrack extends Object implements IAdTracker
    {
        private var _hasAop:Array;
        private var _trackNode:String;
        private var _extInfo:Array;
        private var _aopTracker:AOPTracker;
        private var _baseInfo:IBaseInfo;

        public function TdAdTrack()
        {
            _aopTracker = AOPTracker.getInst();
            _hasAop = [AdType.FRONT, AdType.BACK, AdType.PLACARD, AdType.PAUSE, AdType.BACKGROUND, AdType.FRONT_LAST];
            _baseInfo = PageInfo.getInstance();
            return;
        }// end function

        private function aopTrack(param1:IAdThrowInfo, param2:IAdItemInfo) : void
        {
            if (!hasAop(param2))
            {
                return;
            }
            var _loc_3:Array = [];
            if (NodeMap.ins.aopExtInfoNode(_trackNode))
            {
                _loc_3 = aopExtInfo(param1, param2);
            }
            _aopTracker.addAdNewNodeLog(_trackNode, param2, _loc_3);
            var _loc_4:* = NodeMap.ins.getOldNodeLog(param2, _trackNode);
            var _loc_5:String = "aop : ";
            if (_loc_4 && _loc_4 != "")
            {
                _aopTracker.addAdNodeLog(_loc_4, param2.aid, _loc_3);
            }
            else
            {
                _loc_5 = " !!!!! aop old null : ";
            }
            return;
        }// end function

        private function isClick(param1:String) : Boolean
        {
            return param1 == NodeLog.AD_CLICK;
        }// end function

        private function hasAop(param1:IAdItemInfo) : Boolean
        {
            if (_hasAop.indexOf(param1.type) !== -1)
            {
                return true;
            }
            return false;
        }// end function

        private function isSkip(param1:String) : Boolean
        {
            return param1 == NodeLog.AD_SKIP;
        }// end function

        public function adSend(param1:IAdItemInfo, param2:IAdThrowInfo, param3:String, param4:Array) : void
        {
            var _loc_6:String = null;
            _trackNode = param3;
            _extInfo = param4;
            if (!param1)
            {
                return;
            }
            var _loc_5:Boolean = true;
            if (isStart(param3))
            {
                ADTrackSender.getInstance().sendPlay(param2, param1);
            }
            else if (isComplete(param3))
            {
                ADTrackSender.getInstance().sendComplete(param2, param1);
            }
            else if (isEffectiveShow(param3))
            {
            }
            else if (isClick(param3))
            {
                _loc_5 = false;
                if (AdType.BACKGROUND == param1.type || AdType.OVER_LAY == param1.type)
                {
                    _loc_6 = _extInfo.pop();
                }
                if (AdType.CPT_FLOAT == param1.type)
                {
                    ADTrackSender.getInstance().sendCptClick(param2, param1);
                }
                else
                {
                    ADTrackSender.getInstance().sendClick(param2, param1, _loc_6);
                }
            }
            else if (isFloatClick(param3))
            {
                _loc_5 = false;
                ADTrackSender.getInstance().sendClick(param2, param1, "TUDOU_FLOAT");
            }
            else if (isSkip(param3))
            {
                _loc_5 = false;
                ADTrackSender.getInstance().sendSkip(param2, param1);
            }
            if (_loc_5)
            {
                aopTrack(param2, param1);
            }
            errorReport(param1);
            return;
        }// end function

        private function aopExtInfo(param1:IAdThrowInfo, param2:IAdItemInfo) : Array
        {
            var _loc_3:Array = [];
            switch(_trackNode)
            {
                case NodeLog.AD_FRONT_STUFF_REQUEST_ERROR:
                {
                    _loc_3 = [encodeURIComponent(param2.url)];
                    break;
                }
                case NodeLog.AD_FRONT_STUFF_REQUEST_TIMEOUT:
                {
                    _loc_3 = [encodeURIComponent(param2.url)];
                    _loc_3 = _loc_3.concat(_extInfo);
                    break;
                }
                case NodeLog.AD_BG_STUFF_REQUEST_ERROR:
                {
                    _loc_3 = [encodeURIComponent(param2.backUrl)];
                    break;
                }
                case NodeLog.AD_BG_STUFF_REQUEST_TIMEOUT:
                {
                    _loc_3 = [encodeURIComponent(param2.backUrl)];
                    _loc_3 = _loc_3.concat(_extInfo);
                    break;
                }
                case NodeLog.AD_VIDEO_REQUEST_START:
                case NodeLog.AD_VIDEO_REQUEST_ERROR:
                case NodeLog.AD_VIDEO_REQUEST_TIMEOUT:
                case NodeLog.AD_VIDEO_SUB_REQUEST_START:
                case NodeLog.AD_VIDEO_SUB_REQUEST_ERROR:
                {
                    _loc_3 = [encodeURIComponent(param2.videoUrl), PageInfo.getInstance().areaCode];
                    break;
                }
                case NodeLog.AD_VIDEO_PLAYHEADTIME:
                {
                    _loc_3 = _loc_3.concat(_extInfo, [param2.durationType]);
                    break;
                }
                default:
                {
                    _loc_3 = _extInfo;
                    break;
                    break;
                }
            }
            return _loc_3;
        }// end function

        public function reset() : void
        {
            return;
        }// end function

        private function errorReport(param1:IAdItemInfo) : void
        {
            if (-1 === [AdType.FRONT, AdType.BACK, AdType.PAUSE, AdType.FRONT_LAST].indexOf(param1.type) || !param1.isVideo)
            {
                return;
            }
            switch(_trackNode)
            {
                case NodeLog.AD_VIDEO_SUB_REQUEST_ERROR:
                {
                    ErrorTracker.send(PageInfo.getInstance().sourceId, ErrorTracker.AD_VIDEO_ERROR, 0, param1.videoUrl);
                    break;
                }
                case NodeLog.AD_VIDEO_REQUEST_TIMEOUT:
                {
                    ErrorTracker.send(PageInfo.getInstance().sourceId, ErrorTracker.AD_VIDEO_TIMEOUT, 0, param1.videoUrl);
                    break;
                }
                default:
                {
                    break;
                    break;
                }
            }
            return;
        }// end function

        private function isFloatClick(param1:String) : Boolean
        {
            return param1 == NodeLog.AD_FLOAT_CLICK;
        }// end function

        private function getTrackNode(param1:String, param2:String) : String
        {
            var _loc_3:* = param2;
            return _loc_3;
        }// end function

        private function isStart(param1:String) : Boolean
        {
            return param1 == NodeLog.AD_SHOW_START;
        }// end function

        private function isEffectiveShow(param1:String) : Boolean
        {
            return param1 == NodeLog.AD_VIDEO_EFFECTIVE_SHOW;
        }// end function

        private function isComplete(param1:String) : Boolean
        {
            return param1 == NodeLog.AD_SHOW_COMPLETE;
        }// end function

    }
}
